<?php
session_start();

if (!$_SESSION['loggedin']) {
    // User not logged in, redirect to login page
    echo "<script language='javascript'>window.location='index.php'</script>";
}
if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'registrar') {
?>
    <html>
        <head>
            <link rel="stylesheet" href="css/style.css" type="text/css" id="" media="print, projection, screen" />
            <script type="text/javascript" src="script/jquery-latest.js"></script>
            <script type="text/javascript" src="script/jquery.validate.js"></script>
            <script type="text/javascript">
                $(document).ready(function(){
                    $("#commentForm").validate({
                        rules: {
                            password: "required",
                            password_again: {
                                equalTo: "#password"
                            }
                        }
                    });
                });

                function confirmLogout()
                {
                    var agree=confirm("Are you sure you want to Logout?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }
                function confirmSubmit()
                {
                    var agree=confirm("Are you sure all fields entered are correct?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }


            </script>

            <title>TUP Course Management</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        </head>

        <body style=" height:100%; width: 100%; background-image: url(image/wall6.jpg);">

            <div id="container" style="height:auto; width: 100%;">

                <div style="background-image: url(image/wall12.jpg); height: 110px; width: auto; ">
                </div>

                <div align="center" style="background-image: url(image/wall11.jpg);  height: 74px; width: auto;">
                    <div align="center" style=" height: 4px; width: auto;"></div>
                    <div align="center" style=" height: 22px; width: auto;">
                        <a href="TMMViewProf.php"><b>Professor Management</b></a>&nbsp;&nbsp;
                        <a href="SMMViewStud.php"><b>Student Management</b></a>&nbsp;&nbsp;
                        <a href="CMMViewCourse.php"><b>[ Course Management ]</b></a>&nbsp;&nbsp;
                        <a href="EMSelectProf.php"><b>Enrollment</b></a>
                    </div>

                    <div align="center" style=" height: 4px; width: auto;"></div>
                    <div align="center" style=" height: 22px; width: auto;">
                        <a  href="CMMAddCourse.php" >[ Add Course ]</a>&nbsp;
                        <a href="CMMViewCourse.php">View Course</a>&nbsp;
                        <a href="CMMSearchCourse.php">Search Course</a>&nbsp;
                    </div>
                    <div  align="left" style="padding-left: 4%; height: 18px; width: 46%;float:left;"><?php echo "<aa class=\"smallw\"><b>Welcome " . $_SESSION['fname'] . ", Last login: " . $_SESSION['lastlogin'] . "</b></aa>"; ?>
                    </div>
                    <div align="right" style="padding-right: 4%; height: 18px; width: 46%;float:left;"><bb>|</bb>
                        <a class="small" href="RegEditProfile.php"><b>Edit Profile</b></a>
                        <bb>|</bb>
                        <a onclick="return confirmLogout()" class="small" href="Logout.php"><b>Logout</b></a>
                        <bb>|</bb></div>
                </div>
                <div align="left" style="padding-left: 4%; height: auto;  float:left; width: 96%;">

                    <h2>Add Course</h2>


                <?php
                if (isset($_POST['submit'])) {
                    $coursetitle = ucwords($_POST['coursetitle']);
                    $coursecode = strtoupper($_POST['coursecode']);
                    $coursedes = $_POST['coursedes'];
                    $section = strtoupper($_POST['section']);
                    $minnum = $_POST['minnum'];
                    $maxnum = $_POST['maxnum'];
                    $room = ucwords($_POST['room']);
                    $year = $_POST['year'];
                    $semester = $_POST['term'];
                    $starttime = $_POST['starttime'];
                    $endtime = $_POST['endtime'];
                    $units = $_POST['units'];
                    $codesec = $coursecode . $section . $semester . $year;
                    $blank = "blank";
                    $numeet = $_POST['numeet'];

//echo "afasfafjhn";
//coursetitle coursecode coursedes numsec letter startnum minnum maxnum room

                    include 'DBLibrary/config.php';
                    include 'DBLibrary/openDB.php';

                    mysql_select_db($dbname);

                    $query = "INSERT INTO coursetable(teacher, coursetitle, coursecode, description, section, codesecsemyear, minnumstud, manumstud, totalstud, room, semester, year, starttime, endtime, units, num_of_meeting, validate )
                    VALUES('$blank', '$coursetitle', '$coursecode', '$coursedes', '$section', '$codesec','$minnum', '$maxnum','0','$room', '$semester', '$year', '$starttime', '$endtime', '$units', '$numeet', 'activated' ) ";

                    $queryvalue = mysql_query($query);
                    if ($queryvalue == 0) {
                        echo "<a class=\"red\">" . mysql_errno($conn) . ": " . mysql_error($conn) . "</a>\n";
                        echo "<h3 class=\"err\">Input failed</h3>";
                    } else if ($queryvalue == 1)
                        echo "<h3 class=\"err\">Course has been added</h3>";

                    include 'DBLibrary/closeDB.php';
                } ?>


                <h3>Course Information</h3>
                <form onsubmit="return confirmSubmit()" class="cmxform" id="commentForm" method="post" action="CMMAddCourse.php">
                    <p>Course Title &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<input id="coursetitle" type="text" name="coursetitle" value="" size="100" maxlength="100" class="required" minlength="1"/><br>
                        <br>Course Code &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<input id="coursecode" type="text" name="coursecode" value="" size="7" maxlength="7" class="required" minlength="7"/><br>
                        <br>Course Description  :&nbsp;<input id="coursedes" type="text" name="coursedes" value="" size="100" maxlength="128" class="required" minlength="1"/><br>
                        <br>Section &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<input id="section" type="text" name="section" value="" size="5" maxlength="5" class="required" minlength="1"/><br>
                        <br>Minimum Student &nbsp;&nbsp;&nbsp;:&nbsp;<input id="minnum" type="text" name="minnum" value="" size="2" maxlength="2" class="required" minlength="1" max="45" min="1"/><br>
                        <br>Maximum Student&nbsp;&nbsp;&nbsp;:&nbsp;<input id="maxnum" type="text" name="maxnum" value="" size="2" maxlength="2" class="required" minlength="1" max="45" min="1"/><br>
                        <br>Term &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<select id="term" class="required" name="term" style="margin-left: 4px;" tabindex="8">
                            <option value="">- Select Term -</option>
                            <option value="1st Term" >1st Term</option>
                            <option value="2nd Term" >2nd Term</option>
                            <option value="3rd Term" >3rd Term</option>
                        </select><br>
                        <br>Year &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<select id="year" class="required" name="year" style="margin-left: 4px;" tabindex="8">
                            <option value="">- Select Year -</option>
                            <option value="2013" >2013</option>
                            <option value="2012" >2012</option>
                            <option value="2011" >2011</option>
                            <option value="2010" >2010</option>
                        </select><br>
                        <br>Room &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<input id="room" type="text" name="room" value="" size="5" maxlength="5" class="required" minlength="1"/><br>
                        <br>Start Time &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<input id="starttime" type="text" name="starttime" value="" size="4" maxlength="4" class="required digits" minlength="1"/><br>
                        <br>End Time &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<input id="endtime" type="text" name="endtime" value="" size="4" maxlength="4" class="required digits" minlength="1"/><br>
                        <br>Units &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<input id="units" type="text" name="units" value="" size="1" maxlength="1" class="required number" minlength="1"/><br>
                        <br>Number of Meetings :&nbsp;<input id="numeet" type="text" name="numeet" value="" size="5" maxlength="2" class="required number" minlength="1"/><br>
                        <br><br><input name="submit" class="submit" type="submit" value="submit"/><input type="reset"  value="Reset"/>
                    </p></form>

            </div>
        </div>

        <div id="footer" style="background-image: url(image/wall11.jpg);clear:both;text-align:center; height: 26px; width:auto;">
            <div align="center" style=" height: 4px; width: auto;"></div>
            <bb class ="small">Copyright &#169; 2011</bb>
        </div>

    </body>
</html>
<?php
            } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'admin') {
                echo "<script language='javascript'>window.location='UMMViewUser.php'</script>";
            } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'professor') {
                echo "<script language='javascript'>window.location='AMMSelectSection.php'</script>";
            }
?>